<?php
namespace App\Http\Controllers;
/**
 * 
 * 旅游线路团购信息控制器
 * 
 * @author maqu
 *
 */
use App\Models\Article;
use App\Models\Topic;
use App\Services\GoodsService;
use App\Services\PintuanService;
use App\Services\TourService;
use App\Services\TourTuanService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class TourTuanController extends BaseController {
    /**
     *
     * @param firstrow
     * @param pagesize
     *
     * @return json
     */
    public function getList($tour_id,TourTuanService $tourTuanService){

        $user_id = Input::get('user_id',0);
        $tuan_type = Input::get('tuan_type',TOUR_TUAN_TYPE_PUBLIC);
        $firstrow = Input::get('firstrow',0);
        $pagesize = Input::get('pagesize',10);

        //获取拼团数据
        $tourTuanListData =$tourTuanService->getTuanList($user_id,$tour_id,$tuan_type,$firstrow,$pagesize);

        if(!$tourTuanListData['result']){
            return $this->failure($tourTuanListData['msg']);
        }

        $tourTuanList = $tourTuanListData['data'];

        return $this->success($tourTuanList);

    }

    /**
     *
     * @param firstrow
     * @param pagesize
     *
     * @return json
     */
    public function getMyList($user_id,TourTuanService $tourTuanService){

        $tuan_state = Input::get('tuan_state','all');//  团状态 全部 all/组团中 waiting/已成团 succeed/组团失败 failed
        $firstrow = Input::get('firstrow',0);
        $pagesize = Input::get('pagesize',10);

        //获取拼团数据
        $tourTuanListData =$tourTuanService->getMyTuanList($user_id,$tuan_state,$firstrow,$pagesize);

        if(!$tourTuanListData['result']){
            return $this->failure($tourTuanListData['msg']);
        }

        $tourTuanList = $tourTuanListData['data'];
        return $this->success($tourTuanList);

    }

    /**
     *
     * 拼团详情
     *
     * @param $tuan_id 旅行团ID
     * @return mixed
     */
    public function getDetail($tuan_id,TourTuanService $tourTuanService){

        $user_id = Input::get('user_id',0);
        //获取拼团数据
        $tourTuanDetailData =$tourTuanService->getTuanDetail($user_id,$tuan_id);

        if(!$tourTuanDetailData['result']){
            return $this->failure($tourTuanDetailData['msg']);
        }
        $tourTuanDetail = $tourTuanDetailData['data'];
        return $this->success($tourTuanDetail);
    }

    //收藏拼团
    /**
     * 收藏拼团
     */
    public function collect($pintuan_id){

        $collect_status=0;

        $user_id = Input::get('user_id');

        //参数编辑
        if (!$pintuan_id || !$user_id){
            return $this->args_invalid();
        }
        
        /* 验证要收藏的拼团是否存在 */
        $count =DB::table('pintuan')
                    ->where('pintuan_id',$pintuan_id)
                    ->count();

        if(!$count){
            return $this->failure('该拼团不存在');
        }

        DB::beginTransaction();
        
        try {
            /* 更新收藏表 */   
            unset($count); 
            $count = DB::table('collect')
                        ->where('user_id',$user_id)
                        ->where('type','pintuan')
                        ->where('item_id',$pintuan_id)->count();
            $message = "收藏成功";
            if(!$count){
                DB::table('collect')->insert(
                    array(
                        'user_id' => $user_id,
                        'type' => 'pintuan',
                        'item_id'=>$pintuan_id,
                        'keyword'=>'',
                        'add_time' =>gmtime())
                );
                
                $collect_status=1;
            } else {
                DB::table('collect')
                    ->where('user_id',$user_id)
                    ->where('type','pintuan')
                    ->where('item_id',$pintuan_id)->delete();
                    
                $collect_status=0;
                $message = "取消收藏";
            }
             
            /* 更新被收藏次数 */           
            unset($count);
            $count = DB::table('collect')
                ->where('type','pintuan')
                ->where('item_id',$pintuan_id)
                ->count();
                
            DB::table('pintuan_statistics')
               ->where('pintuan_id',$pintuan_id)
               ->update(array('collects'=>$count));       
      
            DB::commit();

            return $this->success(['collect_status'=>$collect_status],$message);

        } catch (Exception $e) {
            DB::rollback();
            throw $e;
        }            
    }

    /**
     *
     * 获取常见问题
     *
     * @param $pintuan_id
     *
     * @return mixed
     */
    public function getFqa($pintuan_id){

        $data=['content'=>''];

        $article =Article::find(config('maqu.ARTICLD_ID_FQA',18));
        if($article){

            $html =complete_html(config('maqu.STATIC_RESOURCE_URL'),$article->title,$article->content);
            $data['content']=$html;
            unset($html);
        }

        return $this->success($data);

    }

    /* 更新浏览次数 */
    private function _update_views($id){
        DB::table('pintuan_statistics')->where('pintuan_id',$id)->increment('views');
    }
    
}
